package com.example.image;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;

import android.annotation.SuppressLint;
import android.os.Bundle;

import java.io.FileNotFoundException;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.texeljoy.hteffect.HTEffect;
import com.texeljoy.hteffect.model.HTFilterEnum;
import com.texeljoy.hteffect.model.HTFormatEnum;
import com.texeljoy.hteffect.model.HTItemEnum;
import com.texeljoy.hteffect.model.HTRotationEnum;

public class MainActivity extends AppCompatActivity {

    public Button mybutton = null;
    public ImageView myimageview = null;
    private byte[] imageBytes;//RGBA格式的图片
    private byte[] renderBytes;//RGBA格式的图片
    private AppCompatImageView previewImageView;//用于预览的  imageview
    private Bitmap renderedBitmap;//渲染后的bitmap
    private static int previewWidth;//用于预览的图片大小

    private static int previewHeight;//用于预览的图片高度

    private final Handler uiHanler = new Handler(Looper.getMainLooper());//主线程Handler


    private boolean init = false;

    private Handler handler;
    private final HandlerThread handlerThread = new HandlerThread("render");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // todo: hteffect 初始化
        HTEffect.shareInstance().initHTEffect(this, "<YOUR_APP_ID>", new HTEffect.InitCallback() {
            @Override
            public void onInitSuccess() {

            }

            @Override
            public void onInitFailure() {

            }
        });

        setContentView(R.layout.activity_main);
        mybutton = (Button) findViewById(R.id.mybutton);
        myimageview = (ImageView) findViewById(R.id.myimageview);
        mybutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 1);
            }
        });

    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (RESULT_OK == resultCode) {
            Uri uri = data.getData();
            ContentResolver cr = this.getContentResolver();
            try {
                Bitmap bm = BitmapFactory.decodeStream(cr.openInputStream(uri));
                previewWidth = bm.getWidth();
                previewHeight = bm.getHeight();
                // hteffect processImage 方法用
//                imageBytes = BitmapUtils.bitmapToRgba(bm);
                //拷贝图片副本
//                renderBytes = new byte[imageBytes.length];

//                System.arraycopy(imageBytes,
//                        0, renderBytes,
//                        0, imageBytes.length);

                // todo: hteffect 渲染
                if (!init) {
                    Log.e("初始化:", "------");
                    HTEffect.shareInstance().releaseImageRenderer();
                    // HtUICacheUtils.initCache(false);


                    init = HTEffect.shareInstance().initImageRenderer(
                            HTFormatEnum.HTFormatRGBA,
                            previewWidth,
                            previewHeight,
                            HTRotationEnum.HTRotationClockwise0,
                            false,
                            5);
                }
               HTEffect.shareInstance().setFilter(HTFilterEnum.HTFilterBeauty.getValue(), "yuese");

               HTEffect.shareInstance().setARItem(HTItemEnum.HTItemSticker.getValue(), "ht_sticker_effect_smallcat");

                //bitmap图片调用渲染方法
                renderedBitmap = HTEffect.shareInstance().processBitmap(bm);

                // hteffect processImage 方法用
                /*HTEffect.shareInstance().processImage(renderBytes);

                //渲染后的bitmap
                Log.e("渲染:", "-------");

                Log.e("pw:", previewWidth + "px");
                Log.e("ph:", previewHeight + "px");

                renderedBitmap = BitmapUtils
                        .bitmapFromRgba(
                                previewWidth,
                                previewHeight,
                                renderBytes
                        );
*/

                myimageview.setImageBitmap(renderedBitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void render(Boolean bool) {
        if (imageBytes == null) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                //拷贝图片副本
                renderBytes = new byte[imageBytes.length];

                System.arraycopy(imageBytes,
                        0, renderBytes,
                        0, imageBytes.length);

                //渲染
                if (!init) {
                    Log.e("初始化:", "------");
                    HTEffect.shareInstance().releaseImageRenderer();
                    // HtUICacheUtils.initCache(false);


                    init = HTEffect.shareInstance().initImageRenderer(
                            HTFormatEnum.HTFormatRGBA,
                            previewWidth,
                            previewHeight,
                            HTRotationEnum.HTRotationClockwise0,
                            false,
                            5);
                }
                // //Log.d(TAG, mSensorDegrees+"");
                // renderedBitmap = HTEffect.shareInstance().processBitmap(previewBitmap);
                HTEffect.shareInstance().processImage(renderBytes);


                //渲染后的bitmap
                Log.e("渲染:", "-------");

                Log.e("pw:", previewWidth + "px");
                Log.e("ph:", previewHeight + "px");

                renderedBitmap = BitmapUtils
                        .bitmapFromRgba(
                                previewWidth,
                                previewHeight,
                                renderBytes
                        );

                uiHanler.post(new Runnable() {
                    @Override
                    public void run() {
                        //显示
                        previewImageView.setImageBitmap(
                                        renderedBitmap
                                );

                    }
                });
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.activity_demo_get_image, menu);
        return true;
    }
}